package com.microcity.mvvmframe.net;


import com.microcity.mvvmframe.BuildConfig;
import com.microcity.mvvmframe.app.App;
import com.microcity.mvvmframe.net.api.OneApi;
import com.microcity.mvvmframe.net.api.TwoApi;
import com.microcity.mvvmframe.net.interceptor.HttpCacheInterceptor;
import com.microcity.mvvmframe.net.interceptor.HttpLoggingInterceptor;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.schedulers.Schedulers;

/**
 * Created by Darksiderl on 2017/4/20.
 */
public class Net {

    // realBase 正式基址
    public static final String REALBASE = "http://106.14.77.13:8090/";
    // testBase 测试基址
    public static final String TESTBASE = "http://106.14.77.13:8090/";

    private static Net instance;
    private HttpLoggingInterceptor log;
    private Cache cache;
    private HttpCacheInterceptor cacheInterceptor;
    private Retrofit retrofit;

    public Net() {
        if (retrofit == null) {
            log = new HttpLoggingInterceptor();
            // 指定缓存路径,缓存大小100Mb
            File cacheFile = new File(App.getsContext().getCacheDir(), "HttpCache");
            cache = new Cache(cacheFile, 1024 * 1024 * 100);
            cacheInterceptor = new HttpCacheInterceptor();
            if (BuildConfig.DEBUG) {
                log.setLevel(HttpLoggingInterceptor.Level.BODY);
            } else {
                log.setLevel(HttpLoggingInterceptor.Level.NONE);
            }
        }
    }


    public static Net get() {
        if (instance == null) {
            synchronized (Net.class) {
                if (instance == null) {
                    instance = new Net();
                }
            }
        }
        return instance;
    }
    //根据自己的情况去设置catch
    public OneApi oneService() {
        retrofit = new Retrofit.Builder()
                .baseUrl(REALBASE + NetConstant.ONEPATH)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        //错误重连
                        .retryOnConnectionFailure(true)
                        .addInterceptor(log)
                        .cache(cache)
                        .addInterceptor(cacheInterceptor)
                        .build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                .build();
        return retrofit.create(OneApi.class);
    }

    public TwoApi twoService() {
        retrofit = new Retrofit.Builder()
                .baseUrl(REALBASE + NetConstant.TWOPATH)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .readTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(10, TimeUnit.SECONDS)
                        //错误重连
                        .retryOnConnectionFailure(true)
                        .addInterceptor(log)
                        .cache(cache)
                        .addInterceptor(cacheInterceptor)
                        .build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                .build();
        return retrofit.create(TwoApi.class);
    }
}
